/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.client.form;

import java.util.ArrayList;

import org.jb.sharedbills.client.resources.Resources;
import org.jb.sharedbills.client.stores.MemberStore;
import org.jb.sharedbills.shared.model.Event;
import org.jb.sharedbills.shared.model.Group;
import org.jb.sharedbills.shared.model.Member;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.widget.CheckBoxListView;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * 
 * @author jeremie
 *
 */
public class GroupDialog extends Dialog {

	/**
	 * 
	 * @param event
	 * @param group
	 */
	public GroupDialog(Event event, final Group group ) {
		this.event = event;
		this.group = group;
		
		setIcon( AbstractImagePrototype.create(Resources.INSTANCE.users()) );
		
		setBodyBorder(false);  
	    setHeading("Groupe");  
	    setWidth(400);  
	    setHeight(400);  
		setModal( true );
		setLayout( new FormLayout() );
		setButtons(Dialog.OKCANCEL);
		setHideOnButtonClick(true);
		setResizable(false);
		
		name = new TextField<String>();  
		name.setFieldLabel("Nom");  
		name.setAllowBlank(false);  
	    add(name, new FormData("100%"));
	    
	    setFocusWidget(name);
	    
	    checkListView = new CheckBoxListView<BeanModel>();
	    store = new MemberStore( event );
	    checkListView.setStore( store );
	    checkListView.setDisplayProperty("name");
	    checkListView.setHeight( 300 );
	    checkListView.setBorders(false);
	    add( checkListView );
	    
	    if (group != null) {
	    	name.setValue( group.getName() );
	    }
	    	
	    if (group != null) {
	    	
	    	DeferredCommand.addCommand( new Command() {
				@Override
				public void execute() {
					for( Member m :  group.getMembers() ) {
			    		checkListView.setChecked( store.getBean(m), true );
			    	}
				}
	    	} );
	    }
	}
	
	/**
	 * 
	 */
	protected void onButtonPressed(Button button) {
		if (button == getButtonById(Dialog.OK)) {
			
			if (group == null) {
				group = new Group();
				event.getGroups().add(group);
			}
			
			group.setName( name.getValue() );
			
			ArrayList<Member> membersRemoved = new ArrayList<Member>();
			for( Member m : new ArrayList<Member>(group.getMembers()) ) {
				boolean found = false;
				
				for( BeanModel b : checkListView.getChecked()) {
					if (m == b.<Member>getBean() ) {
						found = true;
						break;
					}
				}
				
				if (!found) {
					membersRemoved.add(m);
				}
			}
			
			ArrayList<Member> membersAdded = new ArrayList<Member>();
			for( BeanModel b : checkListView.getChecked()) {
				Member member = b.<Member>getBean();
				if (!group.getMembers().contains(member)) {
					membersAdded.add( member );
				}
			}
			group.getMembers().addAll( membersAdded );
			group.getMembers().removeAll( membersRemoved );
		}
		
		store.close();
		
		super.onButtonPressed(button);
	}
	
	/**
	 * 
	 */
	public void disableEdit() {
		name.setReadOnly(true);
		checkListView.disable();
		getButtonById(Dialog.OK).disable();
	}
	
	private MemberStore store;
	private Group group;
	private final Event event;
	private final TextField<String> name;
	private final CheckBoxListView<BeanModel> checkListView;
	
}
